package top.aicey.ai.sparring.processor;

import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

@Component
public class LocalLogIndexContextProcessor implements LogIndexContextProcessor {

    private Map<String, AtomicInteger> incrementMap = new ConcurrentHashMap<>();

    @Override
    public Integer incr(String instanceId) {
        AtomicInteger atomicInteger = incrementMap.getOrDefault(instanceId, new AtomicInteger());

        if (!incrementMap.containsKey(instanceId)){
            incrementMap.put(instanceId,atomicInteger);
        }
        return atomicInteger.intValue();
    }

    @Override
    public void clear(String instanceId) {
        if (!incrementMap.containsKey(instanceId)){
            incrementMap.remove(instanceId);
        }
    }
}
